﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Osh.Media
{
	public class TimelineInfo : ITimelineInfo
	{
		public static TimelineInfo Empty;

		private DateTime? _startTimeUtc = null;
		private DateTime? _endTimeUtc = null;
		private DateTime? _currentTimeUtc = null;
		private DateTime _capturedUtc;

		static TimelineInfo()
		{
			Empty = new TimelineInfo();
			Empty.CapturedUtc = DateTime.MinValue;
		}

		protected TimelineInfo()
		{
			_capturedUtc = DateTime.UtcNow;
		}

		public TimelineInfo(DateTime startTime, DateTime endTime, DateTime currentTime) : base()
		{
			_startTimeUtc = startTime.ToUniversalTime();
			_endTimeUtc = endTime.ToUniversalTime();
			_currentTimeUtc = currentTime.ToUniversalTime();
		}

		#region ITimelineInfo
		public virtual DateTime? StartTime
		{
			get
			{
				if(_startTimeUtc == null)
					return null;
				return _startTimeUtc.Value.ToLocalTime();
			}
			protected set
			{
				if(value == null)
					_startTimeUtc = null;
				else
					_startTimeUtc = value.Value.ToUniversalTime();
			}
		}

		public virtual DateTime? StartTimeUtc
		{
			get { return _startTimeUtc; }
			protected set { _startTimeUtc = value; }
		}

		public virtual DateTime? EndTime
		{
			get
			{
				if(_endTimeUtc == null)
					return null;
				return _endTimeUtc.Value.ToLocalTime();
			}
			protected set
			{
				if(value == null)
					_endTimeUtc = value;
				else
					_endTimeUtc = value.Value.ToUniversalTime();
			}
		}

		public virtual DateTime? EndTimeUtc
		{
			get { return _endTimeUtc; }
			set { _endTimeUtc = value; }
		}

		public virtual DateTime? CurrentTime
		{
			get
			{
				if(_currentTimeUtc == null)
					return null;
				return _currentTimeUtc.Value.ToLocalTime();
			}
			protected set
			{
				if(value == null)
					_currentTimeUtc = null;
				else
					_currentTimeUtc = value.Value.ToUniversalTime();
			}
		}

		public virtual DateTime? CurrentTimeUtc
		{
			get { return _currentTimeUtc; }
			set { _currentTimeUtc = value; }
		}

		public virtual DateTime Captured
		{
			get { return _capturedUtc.ToLocalTime(); }
			set { _capturedUtc = value.ToUniversalTime(); }
		}

		public virtual DateTime CapturedUtc
		{
			get { return _capturedUtc; }
			set { _capturedUtc = value; }
		}
		#endregion ITimelineInfo

		public static bool HasChanged(ITimelineInfo info1, ITimelineInfo info2)
		{
			if(object.ReferenceEquals(info1, info2))
				return false;

			if(info1 == null || info2 == null)
				return false;

			if(info1.CapturedUtc == info2.CapturedUtc)
				return false;

			return info1.CurrentTimeUtc != info2.CurrentTimeUtc ||
				info1.EndTimeUtc != info2.EndTimeUtc ||
				info1.StartTimeUtc != info2.StartTimeUtc;
		}
	}
}
